package com.situ.thread.consumer;

import java.util.LinkedList;

public class Panzi extends Object{
    private LinkedList<Cake> list = new LinkedList<>();

    //盘子无限大  5
    public synchronized void putCake(Cake cake) {
        if (list.size() == 5) {
            try {
                System.out.println(Thread.currentThread().getName() + " putCake() wait");
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        list.addLast(cake);
        notifyAll();
        System.out.println(Thread.currentThread().getName() + " putCake() notifyAll");
    }

    public synchronized Cake getCake() {
        if (list.size() == 0) {
            try {
                System.out.println(Thread.currentThread().getName() + " getCake() wait");
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        Cake cake = list.removeFirst();
        notifyAll();
        System.out.println(Thread.currentThread().getName() + " getCake() notifyAll");
        return cake;
    }
}
